#pragma once
#include <assert.h>
namespace lw
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}
		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
		{}
		vector(size_t n, const T& val = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(n);
			for (int i = 0; i < n; i++)
				push_back(val);
		}
		vector(int n, const T& val = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(n);
			for (int i = 0; i < n; i++)
				push_back(val);
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		
		vector(vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(v.capacity());
			for (const auto& e : v)
				push_back(e);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		void swap(vector<T>& v)
		{
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_end_of_storage, v._end_of_storage);
		}

		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < sz; i++)
						tmp[i] = _start[i];
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}
		void resize(size_t n, const T& val = T()) 
		{
			if (n > capacity())
				reserve(n);
			if (n > size())
			{
				for (size_t i = size(); i < n; i++)
					_start[i] = val;
			}
			_finish = _start + n;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}
		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*_finish = val;
			_finish++;
		}
		void pop_back()
		{
			assert(size() > 0);
			--_finish;
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos <= _finish && pos >= _start);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}

			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				--end;
			}
			*pos = val;
			_finish++;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish && pos >= _start);
			while (pos < _finish - 1)
			{
				*pos = *(pos + 1);
				pos++;
			}
			_finish--;
			return pos;
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};

	void Test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
		v.push_back(4);
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;

		for (auto e : v)
			cout << e << ' ';
		cout << endl;

	}
	void Test_vector2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(30);
		v.push_back(300);

		for (size_t i = 0; i < v.size(); i++)
			cout << v[i] << ' ';
		cout << endl;

		v.push_back(6);
		v.push_back(7);
		v.push_back(8);
		v.push_back(9);
		v.push_back(10);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;

		v.pop_back();
		v.pop_back();
		v.pop_back();
		for (auto e : v)
			cout << e << ' ';
		cout << endl;
	}
	void Test_vector3()
	{
		vector<int> v(5, 10);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;

		string t("hello world");
		vector<char> s(t.begin(), t.end());
		for (auto ch : s)
			cout << ch << ' ';
		cout << endl;

		vector<char> st = s;
		for (auto ch : st)
			cout << ch << " ";
		cout << endl;

		st.push_back('!');
		st.push_back('!');
		st.push_back('!');
		st.push_back('!');

		s = st;
		for (auto ch : s)
			cout << ch << ' ';
		cout << endl;
		for (auto ch : st)
			cout << ch << " ";
		cout << endl;
	}

	void Test_vector4()
	{
		vector<string> words;
		words.push_back("string");
		words.push_back("sort");
		words.push_back("vector");

		vector<string> copy = words;

	}

	void Test_vector5()
	{
		vector<int> v;
		v.resize(6, 20);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;
		v.resize(4);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;

		v.insert(v.begin(), 0);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;
		v.push_back(30);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;

		v.erase(v.begin());
		v.erase(v.end() - 1);
		for (auto e : v)
			cout << e << ' ';
		cout << endl;

	}
}